package com.edu.app.controller.web;

import com.edu.app.bean.core.SysCustomer;
import com.edu.app.bean.core.SysListOfValue;
import com.edu.app.constant.ErrorConstant;
import com.edu.app.constant.SysMessage;
import com.edu.app.service.sys.core.SysCustomerService;
import com.edu.app.service.sys.core.SysListOfValueService;
import com.edu.app.service.sys.core.SysUserService;
import com.edu.app.service.sys.security.SysSecurityService;
import com.edu.app.util.ErrorUtil;
import com.edu.app.util.MsgUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("api/marketing")
@Controller
public class MarketingController {

    @Autowired
    private SysCustomerService sysCustomerService;

    @Autowired
    private SysSecurityService sysSecurityService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysListOfValueService sysListOfValueService;

    @Value("${server.host}")
    private String host;

    @Value("${server.port}")
    private String port;

    @GetMapping("/outside")
    public String marketingOutside(Model map) {
        map.addAttribute("host", host);
        map.addAttribute("port", port);
        return "marketing_potential_customer_roll_in";
    }

    @GetMapping("/thanks")
    public String thanks() {
        return "thanks";
    }


    @PostMapping("/potentialCustomerRollIn")
    @ResponseBody
    public ModelMap addMarketingUser (@RequestBody SysCustomer sysCustomer, @RequestParam String code,
                                      @RequestParam String hashcode){

        if(sysCustomer.getPhone() == null){
            return ErrorUtil.retErrMsg(ErrorConstant.PHONE_CANNOT_BE_NULL);
        }

        //校验验证码的合法性
        if(!sysSecurityService.validateVerifyCode(code, hashcode, sysCustomer.getPhone())){
            return ErrorUtil.retErrMsg(ErrorConstant.INVALID_VERIFICATION);
        }

        try{
            //Mark the current po customer's channel from the web roll in
            Integer id = 0;
            List<SysListOfValue> sysListOfValueList = sysListOfValueService.getObjectListByType("S_LOV_REFER_CHANNEL_MARKETING");
            for(SysListOfValue listOfValue: sysListOfValueList){
                id = listOfValue.getId();
            }
            if(id != 0){
                sysCustomer.setRefer_channel_id(id);
            }

            sysCustomer.initializeSystemColumns("MarketingSurvey");
            Integer i = sysCustomerService.add(sysCustomer);
            if(i > 0){
                return MsgUtil.retMsg(SysMessage.ADD_CUSTOMER_SUCCEED, null, null);
            }else{
                return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
            }
        }catch (org.springframework.dao.DuplicateKeyException e){
            return ErrorUtil.retErrMsg(ErrorConstant.CUSTOMER_PHONE_ALREADY_EXISTS);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }


    /*
    @GetMapping("/outside")
    public String marketingOutside(Model map, @RequestParam String channel_name, @RequestParam String channel_code) {

        if(channel_name == null || channel_code == null){
            return "error";
        }

        map.addAttribute("channel_name", channel_name);
        map.addAttribute("channel_code", channel_code);
        map.addAttribute("host", host);
        map.addAttribute("port", port);
        return "marketing_potential_customer_roll_in";
    }

    @PostMapping("/potentialCustomerRollIn")
    @ResponseBody
    public ModelMap addMarketingUser (@RequestBody SysCustomer sysCustomer, @RequestParam String code,
                                      @RequestParam String hashcode, @RequestParam String channel_name, @RequestParam String channel_code){

        String targetChannelCode = SecurityUtil.getEncryptCode(channel_name, channel_name);
        if(!targetChannelCode.equalsIgnoreCase(channel_code)){
            return MsgUtil.retMsg(SysMessage.INVALID_CHANNEL_USER, null, null);
        }

        SysUser sysUser = sysUserService.getUserByName(channel_name);
        if(sysUser == null){
            return ErrorUtil.retErrMsg(ErrorConstant.USER_NOT_EXISTS);
        }else{
            //sysCustomer.setContributor_user_id(sysUser.getId());
        }

        //校验验证码的合法性
        if(!sysSecurityService.validateVerifyCode(code, hashcode)){
            return ErrorUtil.retErrMsg(ErrorConstant.INVALID_VERIFICATION);
        }

        try{
            Integer i = sysCustomerService.add(sysCustomer);
            if(i > 0){
                return MsgUtil.retMsg(SysMessage.ADD_CUSTOMER_SUCCEED, null, null);
            }else{
                return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
            }
        }catch (org.springframework.dao.DuplicateKeyException e){
            return ErrorUtil.retErrMsg(ErrorConstant.CUSTOMER_PHONE_ALREADY_EXISTS);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }*/
}
